﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ICSharpCode.MSSQLExtensions
{
    public class FoldValueMatch
    {
        public int iStartOffset;
        public int iEndOffset;
        public bool IsValid()
        {
            return ((iStartOffset > -1) && (iEndOffset > -1));
        }
    }

    public class FoldValueList
    {
        List<FoldValueMatch> m_FoldValueMatches = new List<FoldValueMatch>();

        public List<FoldValueMatch> FoldValueMatches
        {
            get { return m_FoldValueMatches; }
            set { m_FoldValueMatches = value; }
        }
        private List<FoldValue> m_lstFoldValues = new List<FoldValue>();

        public FoldValueList()
        {
            m_lstFoldValues.Add(new FoldValue("begin", "end"));
            m_lstFoldValues.Add(new FoldValue("case", "end"));
        }

        internal void CheckWord(int iOffset, string strWord)
        {

            //HCSocketToolsLib.HCSocketSender.Instance().SendUDPMessage(
            //    System.Reflection.MethodBase.GetCurrentMethod()
            //    , " offset " + iOffset + " word |" + strWord + "|");

            for (int i = 0; i < m_lstFoldValues.Count; i++)
            {
                FoldValue oFoldValue = m_lstFoldValues[i];
                if (oFoldValue.IsValidStart(strWord))
                {
                    AddStart(iOffset);
                    break;
                }
                else
                {
                    if (oFoldValue.IsValidEnd(strWord))
                    {
                        AddEnd(iOffset);
                        break;
                    }
                }
            }
        }

        public void AddStart(int iOffset)
        {
            m_FoldValueMatches.Add(new FoldValueMatch { iStartOffset = iOffset, iEndOffset = -1 });
        }

        public void AddEnd(int iOffset)
        {
            for (int i = m_FoldValueMatches.Count - 1; i > -1; i--)
            {
                FoldValueMatch oMatch = m_FoldValueMatches[i];
                if (oMatch.iEndOffset == -1)
                {
                    oMatch.iEndOffset = iOffset;
                    break;
                }
            }
        }

    }
}
